package com.honour.controller.interfaced;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.impl.CouponInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.DateUtil;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.drools.DroolsFactory;

/**  优惠券接口
@Function
@Author zhaozhenhua
@Date   2018-01-23
*/
@Controller
@RequestMapping(value="/couponin")
public class CouponInController extends BaseController{
	
		
	@Resource(name="couponInService")
	private CouponInService couponInService;
	
	/*@Resource(name="businessInService")
	private BusinessInManager businessInService;*/
	
	/**
	 * 根据business_id获取优惠券列表
	 * @Author zhaozhenhua
	 * @date   2018-01-23
	 * @return
	 */
	@RequestMapping(value="/couponlist")
	@ResponseBody
	public Object getAdvertisementList(){
		logBefore(logger, "根据business_id获取优惠券列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("COUPONLIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCouponListById", pd)){	//检查参数
					 List<PageData> list = couponInService.getCouponListById(pd);
					 map.put("pd",list);
						result = (null == list) ?  "02" :  "01";
					}else {
						result = "03";
					}	
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 用户领取优惠券
	 * @Author zhaozhenhua
	 * @date   2018-01-23
	 * @return
	 */
	@RequestMapping(value="/addcoupon")
	@ResponseBody
	public Object addCoupon(){
		logBefore(logger, "用户领取优惠券");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("ADDCOUPON", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addCoupon", pd)){	//检查参数
					 PageData p = couponInService.getCouponUserByDoubelId(pd);
					 if(p!=null){
						 result="已领取";
					 }else{
						 pd.put("COUPONUSER_ID", this.getSnowId());
						 couponInService.saveCoupon(pd);
						 //使用Drools规则
						 PageData coupon = couponInService.findById(pd);
						 coupon.put("addCoupon", "addCoupon");
						 DroolsFactory.excuteCoupon(coupon);
						 couponInService.edit(coupon);
						 result  ="01";
					 }
					}else {
						result = "03";
					}	
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据店铺id获取可用的优惠券
	 * @Author zhaozhenhua
	 * @date   2018-01-23
	 * @return
	 */
	@RequestMapping(value="/getcouponsuser")
	@ResponseBody
	public Object getCouponsUser(){
		logBefore(logger, "根据店铺id获取可用的优惠券");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("COUPONSUSER", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCouponListById", pd)){	//检查参数
					 List<PageData> list = couponInService.getCouponsById(pd);
					 map.put("pd",list);
						result = (null == list) ?  "02" :  "01";
					}else {
						result = "03";
					}	
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台商家添加优惠券
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月27日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/busiaddcoupon")
	@ResponseBody
	public Object busiAddCoupon(){
		logBefore(logger, "后台商家添加优惠券");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("BUSIADDCOUPON", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("busiAddCoupon", pd)){	//检查参数
					//PageData business = businessInService.getBusinessLogo(pd);
					//ImageUtil.convert(business.getString("SHOWIMG"));
					//pd.put("SHOWIMG", ImageUtil.showimg);
					pd.put("COUPON_ID", this.getSnowId());
					//代金券状态：1-可用
					pd.put("COUPON_STATE", "1");
					//商户此次发放凭据号（格式：商户id+日期+流水号），商户侧需保持唯一性
					pd.put("PARTNER_TRADE_NO", pd.getString("BUSINESS_ID")+DateUtil.getdaySFM()+UUID.randomUUID().toString().replace("-", ""));
					couponInService.busiAddCoupon(pd);
					map.put("COUPON_ID", pd.get("COUPON_ID"));
					result = "01";
				}else {
					result = "03";
				}	
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台根据id获取优惠券
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月28日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getcoupon")
	@ResponseBody
	public Object getCoupons(){
		logBefore(logger, "后台根据id获取优惠券");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("COUPON", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCoupon", pd)){	//检查参数
					 PageData coupon = couponInService.findById(pd);
					 map.put("pd",coupon);
						result = (null == coupon) ?  "02" :  "01";
					}else {
						result = "03";
					}	
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台根据id修改优惠券
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年2月28日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/updcoupon")
	@ResponseBody
	public Object updCoupons(){
		logBefore(logger, "后台根据id修改优惠券");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPDCOUPON", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCoupon", pd)){	//检查参数
					couponInService.edit(pd);
					result =  "01";
				}else {
					result = "03";
				}	
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	

}
